package com.hxzy.controller.admin.category;

import com.github.pagehelper.PageInfo;
import com.hxzy.common.annotates.RequiredRole;
import com.hxzy.common.constants.MarketConstants;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.resp.R;
import com.hxzy.common.util.MarketThreadLocalUtil;
import com.hxzy.controller.admin.category.req.CategoryClassAddReq;
import com.hxzy.controller.admin.category.req.CategoryClassEditReq;
import com.hxzy.controller.admin.category.req.CategoryClassSearchReq;
import com.hxzy.controller.admin.category.resp.CategoryClassResp;
import com.hxzy.controller.admin.login.resp.AdminLoginResp;
import com.hxzy.service.CategoryClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "后台-商品分类Api")
@RestController
@RequestMapping(value = "/api/category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryClassService categoryClassService;

    @ApiOperation(value = "商品分类查询")
    @GetMapping(value = "/data")
    public R<PageInfo<CategoryClassResp>> search(@Valid CategoryClassSearchReq req){
        AdminLoginResp adminLoginResp = MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get();
        //商家(在后强制赋值)
        if(!adminLoginResp.isAdmin()){
            req.setMerchantId(adminLoginResp.getId());
        }
        super.startPage(req);
       List<CategoryClassResp> list=this.categoryClassService.search(req);
       return super.buildPageInfo(list);
    }


    @RequiredRole(role = MarketConstants.ACCOUNT_MERCHANDISER_TYPE)
    @ApiOperation(value = "新增商品分类")
    @PostMapping
    public R addData(@RequestBody @Valid CategoryClassAddReq req){
        boolean result=this.categoryClassService.addData(req);
        return result?R.ok():R.build(AckCode.DATABASE_ERROR);
    }

    @RequiredRole(role = MarketConstants.ACCOUNT_MERCHANDISER_TYPE)
    @ApiOperation(value = "修改商品分类")
    @PutMapping
    public R updateData(@RequestBody @Valid CategoryClassEditReq req){
        boolean result=this.categoryClassService.editData(req);
        return result?R.ok():R.build(AckCode.DATABASE_ERROR);
    }


}
